<?php
require_once("../../config/config.inc.php");
require_once($CFG_REAL_CLASSES."/json.class.php");
$json = new Services_JSON();

$sAction    = fn_getParam("strAction","");
$sProjectID = fn_getParam("strProjectID",-1);

if ($sAction==ACTION_INSERT || $sAction==ACTION_UPDATE) {
    $sProjectCode  = fn_getParam("strProjectCode","");
    $sProjectType  = fn_getParam("strProjectType","");
    $sProjectName  = fn_getParam("strProjectName","");
    $sStartDate    = fn_getParam("strStartDate","");
    $sFinishDate   = fn_getParam("strFinishDate","");
    $sPriority     = fn_getParam("strPriority","");
    $sManager      = fn_getParam("strManager",$SEC->sUserName);
    $sContent      = fn_getParam("strContent","");
    $sIsPersonal   = fn_getParam("strIsPersonal","");
    $sWorkday      = fn_getParam("strWorkDay","");
    $sAMStartTime  = fn_getParam("strAMStartTime","");
    $sAMEndTime    = fn_getParam("strAMEndTime"  ,"");
    $sPMStartTime  = fn_getParam("strPMStartTime","");
    $sPMEndTime    = fn_getParam("strPMEndTime"  ,"");
    $sWorkTime     = fn_getParam("strWorkTime"   ,"");
    $sWorkTime     = trim(str_replace("小时","",$sWorkTime));

    if ($sAction==ACTION_UPDATE) { //update it
        $sql = "UPDATE $TABLE_PROJECT SET ProjectCode='".$sProjectCode."',CONTENT='".$sContent;
        $sql.= "',ProjectType='".$sProjectType."',ProjectName='".$sProjectName."',";
        $sql.= "StartDate='".$sStartDate."',FinishDate='".$sFinishDate."',ISPERSONAL='".$sIsPersonal."',";
        $sql.= "Priority='".$sPriority."',Manager='".$sManager."',Workday='".$sWorkday."',";
        $sql.= "AMStartTime='".$sAMStartTime."',AMEndTime='".$sAMEndTime."',";
        $sql.= "PMStartTime='".$sPMStartTime."',PMEndTime='".$sPMEndTime."',";
        $sql.= "DayWorkTime='".$sWorkTime."' ";
        $sql.= "WHERE ID='".$sProjectID."'";
        $DB->query($sql);
        $sMesg = "项目信息保存成功。";
    } else {
        //add project content
        $sql = "INSERT INTO $TABLE_PROJECT(USERID,USERNAME,PROJECTCODE,PROJECTNAME,PROJECTTYPE,STARTDATE,";
        $sql.= "FINISHDATE,PRIORITY,MANAGER,CONTENT,WORKDAY,AMSTARTTIME,AMENDTIME,PMSTARTTIME,PMENDTIME,";
        $sql.= "ISPERSONAL,DAYWORKTIME,CREATEDATE) ";
        $sql.= "VALUES('".$SEC->sUserID."','".$SEC->sUserName."','".$sProjectCode."','".$sProjectName."','";
        $sql.= $sProjectType."','".$sStartDate."','".$sFinishDate."','".$sPriority."','".$sManager."','";
        $sql.= $sContent."','".$sWorkday."','".$sAMStartTime."','".$sAMEndTime."','".$sPMStartTime."','";
        $sql.= $sPMEndTime."','".$sIsPersonal."','".$sWorkTime."',NOW())";
        $DB->query($sql);
        $sMesg = "项目信息添加成功。";
    }
    echo $json->encode(array('success'=>true,'msg'=>$sMesg));
} else
if ($sAction == ACTION_VIEW && $sProjectID != "") {
    $sql = "SELECT USERID,USERNAME,PROJECTCODE,PROJECTNAME,PROJECTTYPE,STARTDATE,FINISHDATE,PRIORITY,";
    $sql.= "MANAGER,CONTENT,WORKDAY,DAYWORKTIME,AMSTARTTIME,AMENDTIME,PMSTARTTIME,PMENDTIME,ISPERSONAL,CREATEDATE ";
    $sql.= "FROM $TABLE_PROJECT ";
    $sql.= "WHERE ID='".$sProjectID."'";
    $stmt = $DB->query($sql);
    $aProject = array();
    if ($row = $DB->fetchArray($stmt)) {
        $aProject["strProjectName"]= $row["PROJECTNAME"];
        $aProject["strProjectCode"]= $row["PROJECTCODE"];
        $aProject["strProjectType"]= $row["PROJECTTYPE"];

        $aProject["strStartDate"]  = fn_fmDatetime($row["STARTDATE"]);
        $aProject["strFinishDate"] = fn_fmDatetime($row["FINISHDATE"]);
        $aProject["strManager"]    = $row["MANAGER"];
        $aProject["strContent"]    = $row["CONTENT"];
        $aProject["strCreateUser"] = $row["USERNAME"];
        $aProject["strCreateDate"] = $row["CREATEDATE"];
        $aProject["strPriority"]   = $row["PRIORITY"];
        $aProject["strIsPersonal"] = $row["ISPERSONAL"];
        $aProject["strWorkDay"]    = $row["WORKDAY"];
        $aProject["strWorkTime"]   = $row["DAYWORKTIME"]." 小时";
        $aProject["strAMStartTime"]= $row["AMSTARTTIME"];
        $aProject["strAMEndTime"]  = $row["AMENDTIME"];
        $aProject["strPMStartTime"]= $row["PMSTARTTIME"];
        $aProject["strPMEndTime"]  = $row["PMENDTIME"];
    }
    $DB->freeResult($stmt);

    //只要当前用户是项目负责人，或当前用户是系统管理且项目的开放级别是在单位内公开的，才可以编辑。
    //if ($aProject["MANAGER"]==$SEC->sUserName || $SEC->sRole == RIGHT_ADMIN &&
    //    ($aProject["ISPERSONAL"]=="1" || $aProject["ISPERSONAL"]=="2" || $aProject["ISPERSONAL"]=="3")) {
    //    $isEnableEdit = true;
    //} else {
    //    $isEnableEdit = false;
    //}
    echo $json->encode(array('success'=>true,'total'=>"1",'data'=>$aProject,'right'=>""));

} else
if ($sAction == ACTION_DELETE && $sProjectID > 0) { //delete project
    $conn = $DB->tconnect();
    $DB->tbegin($conn);
    //delete task in first
    $sql = "DELETE FROM $TABLE_PRETASK WHERE TASKID IN (SELECT ID FROM $TABLE_TASK WHERE PROJECTID='".$sProjectID."')";
    $DB->tquery($sql,$conn);
    $sql = "DELETE FROM $TABLE_TASK_RESOURCE WHERE TASKID IN (SELECT ID FROM $TABLE_TASK WHERE PROJECTID='".$sProjectID."')";
    $DB->tquery($sql,$conn);
    $sql = "DELETE FROM $TABLE_TASK WHERE PROJECTID='".$sProjectID."'";
    $DB->tquery($sql,$conn);

    $sql = "DELETE FROM $TABLE_QUESTIONLOG WHERE PROJECTID='".$sProjectID."'";
    $DB->tquery($sql,$conn);
    $sql = "DELETE FROM $TABLE_PROJECT WHERE ID='".$sProjectID."'";
    $DB->tquery($sql,$conn);

    $DB->tcommit($conn);
    $DB->tclose($conn);
    $sMesg = "您选择的项目及其相关信息已经成功删除。";
    echo $json->encode(array('success'=>true,'msg'=>$sMesg));
} else
if ($sAction == "projectlist") {
    $iStart = fn_getParam("start","0");
    $iPageSize  = fn_getParam("limit","20");
    $isMyCreate = fn_getParam("chkCreate",0);
    $isMyManage = fn_getParam("chkManage",0);
    $isMyDo     = fn_getParam("chkDo",0);

    $aProject = array();
    $aRight = $DICT->getDictArray("BASE_SECURITY",false); //1-全员开放,2-只对好友开放 ,3-完全保密

    $sqlWhere = "FROM $TABLE_PROJECT ";
    $sqlWhere.= "WHERE (1=2";
    if ($isMyCreate == 1) {
        $sqlWhere.= " OR USERNAME='".$SEC->sUserName."'";
    }
    if ($isMyManage == 1){
        $sqlWhere.= " OR MANAGER='".$SEC->sUserName."'";
    }
    if ($isMyDo == 1) {
        $sqlWhere.= " OR ID IN (SELECT DISTINCT PROJECTID FROM $TABLE_TASK WHERE ID IN (SELECT DISTINCT TASKID FROM $TABLE_TASK_RESOURCE WHERE USERNAME='".$SEC->sUserName."'))";
    }
    $sqlWhere.= ")";

    $iRowCount = $DB->getFirstField("SELECT COUNT(1) ".$sqlWhere);

    $sql = "SELECT ID,USERNAME,PROJECTCODE,PROJECTNAME,PROJECTTYPE,";
    $sql.= "STARTDATE,FINISHDATE,PRIORITY,MANAGER,CREATEDATE,ISPERSONAL ";
    $sql.= $sqlWhere;
    $sql.= " ORDER BY CREATEDATE DESC";
    $sql.= " LIMIT $iStart,$iPageSize";
    ///fn_log("sql:".$sql);
    $stmt= $DB->query($sql);
    while ($row = $DB->fetchRow($stmt)) {
        $aProject[] = array(
            "sID"=>$row["ID"],"pCode"=>$row["PROJECTCODE"],
            "pName"=>$row["PROJECTNAME"],"pType"=>$row["PROJECTTYPE"],
            "sDate"=>$row["STARTDATE"],"fDate"=>$row["FINISHDATE"],
            "sPri"=>$row["PRIORITY"],"sMan"=>$row["MANAGER"],
            "cDate"=>$row["CREATEDATE"],"cUser"=>$row["USERNAME"],
            "isPer"=>$aRight[$row["ISPERSONAL"]]
            );
    }
    $DB->freeResult($stmt);
    ///fn_log("result:".print_r($aProject,true));
    if (count($aProject[0])>0) {
        echo $json->encode(array('success'=>true,'total'=>$iRowCount,'data'=>$aProject));
    } else {
        echo $json->encode(array('success'=>true,'total'=>"0",'data'=>""));
    }
    unset($aProject);
}
?>